define(['knockout', 'util', 'aFormField', 'store'], function (ko, Util, AFormField, Store) {
    function ViewModel(params) {
        this.callParent(params);
        var scope = this;

        if (scope.field.option && scope.field.option.model) {
            var store = new Store({
                model: scope.field.option.model,
                pageSize: 0,
                rawData:true,
                    onLoadComplete: function () {
                    scope.field._options(store.records());
                }
            })
        }
        else {

        }

        ko.computed(function () {
            if(scope.field._options().length == 0) return;

            var displayName = scope.field.option.displayField;
            var ids = scope.record[scope.field.name]();
            var texts = ids.map(function (id) {
                return scope.field._options().findItemByField({value: id, name: scope.field.option.valueField})[displayName];
            });

            scope.record._texts = texts;
            scope.field.__displayValue(texts.join(' , '));
        });
    }

    ViewModel.extend(AFormField);

    return ViewModel;
});
